import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { IView } from "../../mvc/interfaces/IView";
import { NPCShopMallModule } from "../../../modules/NPCShopMallModule";
import { WindowEvent } from "../../component/window/WindowEvent";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { ECmdCell } from "../../../Message/Command/ECmdCell";
import { SMoney } from "../../../Message/Game/SMoney";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { ItemConfig } from "../resource/ItemConfig";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { NpcShopInfo } from "../scene/player/info/NpcShopInfo";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SNpcShopItemUpdate } from "../../../Message/Public/SNpcShopItemUpdate";
import { EntityUtil } from "../scene/layer/utils/EntityUtil";
import { INPCShopMallModule } from "../../../modules/interfaces/INPCShopMallModule";
type int = number;
//class NpcShopMallController
    
    export  class NpcShopMallController extends Controller
    {
        constructor()
        {
            
            super();Dispatcher.addEventListener(EventName.OpenNpcShopMall, this.onOpenNpcShopMallHandler);
            Dispatcher.addEventListener(EventName.ShopBuyNpcItem, this.onShopBuyNpcItemHandler);
            Dispatcher.addEventListener(EventName.ShopBuyNpcItemSuccess, this.onShopBuyNpcItemSuccessHandler);
            return;
        }

        public /* override */ get view(): IView
        {
            return this.initView();
        }

        protected /* override */ initView(): IView
        {
            if (null == this._npcShopMallModule) 
            {
                this._npcShopMallModule = new NPCShopMallModule();
                this._npcShopMallModule.addEventListener(WindowEvent.SHOW, this.onAddedToStageHandler);
            }
            return this._npcShopMallModule;
        }

        /* internal  */onAddedToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            this.onMoneyUpdateHandler();
            return;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdateHandler);
            NetDispatcher.addCmdListener(ECmdCell._ECmdCellUpdateShopItem, this.onNpcShopBroadCastHandler);
            return;
        }

        /* internal  */onMoneyUpdateHandler(arg1: SMoney=null): void
        {
            if (this._npcShopMallModule && this._npcShopMallModule.isHide == false) 
            {
                this._npcShopMallModule.update(cache.role.money);
            }
            return;
        }

        /* internal  */onShopBuyNpcItemHandler(arg1: DataEvent): void
        {
            var loc1=this._npcShopInfo.npcShop.entityId;
            var loc2=arg1.data["itemCode"] as int;
            var loc3=arg1.data["itemCount"] as int;
            GameProxy.shopProxy.buyNpcShopItem(loc1, loc2, loc3);
            return;
        }

        /* internal  */onShopBuyNpcItemSuccessHandler(arg1: DataEvent): void
        {
            var loc1="";
            var loc2=arg1.data["itemCode"] as int;
            var loc3=arg1.data["itemCount"] as int;
            var loc4;
            if ((loc4 = ItemConfig.instance.getInfoByCode(loc2)) && loc4.item) 
            {
                loc1 = loc4.item.name;
            }
            MsgManager.showRollTipsMsg(Language.getStringByParam(53001, loc3) + loc1);
            return;
        }

        /* internal  */onOpenNpcShopMallHandler(arg1: DataEvent): void
        {
            this._npcShopInfo = arg1.data as NpcShopInfo;
            this.initView();
            if (this._npcShopInfo) 
            {
                this._npcShopMallModule.updateDataProvider(this._npcShopInfo.npcShop.shopItem.shopItems);
            }
            this._npcShopMallModule.show();
            return;
        }

        /* internal  */onNpcShopBroadCastHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SNpcShopItemUpdate;
            if (loc1 && this._npcShopInfo && EntityUtil.toString(loc1.entityId) == EntityUtil.toString(this._npcShopInfo.npcShop.entityId)) 
            {
                if (this._npcShopMallModule) 
                {
                    this._npcShopMallModule.updateShopItem(loc1);
                }
            }
            return;
        }

        private /* var */_npcShopMallModule: INPCShopMallModule;

        private /* var */_npcShopInfo: NpcShopInfo;
    }
